<template>
  <div class="ahp-detail-container">
    <el-dialog title="层次分析法" :visible.sync="dialogVisible" width="80vw" :close-on-click-modal="false" @closed="closeDialog">
      <div class="ahp-detail-content">

        <div class="ahp-detail-content__item" v-for="(item, index) in ahpData" :key="index">
          <div class="ahp-detail-content__item_topline">
            <i class="el-icon-error" style="color: red" v-if="ahpNumData[index].cr > 0.1"></i>
            最大特征值 {{ getFour(ahpNumData[index].max) }} 一致性CR
            {{ getFour(ahpNumData[index].cr) }} CI
            {{ getFour(ahpNumData[index].ci) }}
          </div>
          <el-table :data="item" border>
            <el-table-column prop="topic" width="180"> </el-table-column>
            <el-table-column v-for="(itemChild, indexChild) in item" :key="indexChild" prop="topic"
              :label="itemChild.topic">
              <template slot-scope="scope">
                <el-select v-model="ahpMult[index][scope.$index][indexChild]" :disabled="scope.$index >= indexChild"
                  placeholder="请选择" @change="ahpComp">
                  <el-option v-for="itemOptions in ahpOptions" :key="itemOptions.value" :label="itemOptions.label"
                    :value="itemOptions.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
          </el-table>
        </div>

      </div>
      <div slot="footer">
        <el-button class="jtyj-btn-cancel" @click="dialogVisible = false">取消</el-button>
        <el-button class="jtyj-btn-primary" @click="ahpSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AHP from "@/utils/ahp/AHP";
export default {
  name: "AhpDetail",
  components: {
  },
  data() {
    return {
      dialogVisible: false,
      // jsMind图表数据
      jsmindData: [],
      // 表格数据
      ahpData: [],
      // 层次分析法二维数组
      ahpMult: [],
      // 层次分析法各类数值
      ahpNumData: [],
      // 层次分析法选项
      ahpOptions: [
        {
          value: 1,
          label: "1:同样重要",
        },
        {
          value: 2,
          label: "2:同等重要",
        },
        {
          value: 3,
          label: "3:稍微重要",
        },
        {
          value: 4,
          label: "4:稍重重要",
        },
        {
          value: 5,
          label: "5:比较重要",
        },
        {
          value: 6,
          label: "6:特别重要",
        },
        {
          value: 7,
          label: "7:非常重要",
        },
        {
          value: 8,
          label: "8:超级重要",
        },
        {
          value: 9,
          label: "9:绝对重要",
        },
        {
          value: 1 / 2,
          label: "1/2:同等不重要",
        },
        {
          value: 1 / 3,
          label: "1/3:稍微不重要",
        },
        {
          value: 1 / 4,
          label: "1/4:稍重不重要",
        },
        {
          value: 1 / 5,
          label: "1/5:比较不重要",
        },
        {
          value: 1 / 6,
          label: "1/6:特别不重要",
        },
        {
          value: 1 / 7,
          label: "1/7:非常不重要",
        },
        {
          value: 1 / 8,
          label: "1/8:超级不重要",
        },
        {
          value: 1 / 9,
          label: "1/9:绝对不重要",
        },
      ],
      // mindId
      mindId: "",
    };
  },
  methods: {
    // 初始化
    init(initData) {
      this.dialogVisible = true;
      this.jsmindData = JSON.parse(initData.indexContent);
      this.mindId = initData.id;
      // 循环获取多个表格
      this.getTable(this.jsmindData);

      // 给表格每个选项赋值
      this.getOneData();
      if (this.jsmindData.ahp) {
        // 判断已经计算完是否有改动
        let isChange = true;
        if (this.ahpData.length !== this.jsmindData.ahp.length) {
          isChange = false;
        } else {
          this.ahpData.map((item, index) => {
            if (item.length !== this.jsmindData.ahp[index].length) {
              isChange = false;
            }
          });
        }
        if (isChange) {
          this.ahpMult = this.jsmindData.ahp;
        }
        // 开始计算一下层次分析的数据
        this.ahpComp();
      } else {
        // 开始计算一下层次分析的数据
        this.ahpComp();
      }

      console.log(this.ahpData);
    },
    // 截取字符串
    getFour(num) {
      return String(num).replace(/^(.*\..{4}).*$/, "$1");
    },
    // 输入框的获取数据
    querySearch(queryString, cb) {
      var ahpOptions = this.ahpOptions;
      var results = queryString
        ? ahpOptions.filter(this.createFilter(queryString))
        : ahpOptions;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (ahpOptions) => {
        return ahpOptions.value === queryString;
      };
    },
    // 获取图表数据
    getZbtxData() {
      const params = {
        indexId: this.$route.query.indexId,
      };
      getZbtxData(params).then((response) => {
        if (response.code == 200 && response.rows) {
          this.jsmindData = JSON.parse(response.rows[0].indexContent);
          this.mindId = response.rows[0].id;
          // 循环获取多个表格
          this.getTable(this.jsmindData);

          // 给表格每个选项赋值
          this.getOneData();
          if (this.jsmindData.ahp) {
            // 判断已经计算完是否有改动
            let isChange = true;
            if (this.ahpData.length !== this.jsmindData.ahp.length) {
              isChange = false;
            } else {
              this.ahpData.map((item, index) => {
                if (item.length !== this.jsmindData.ahp[index].length) {
                  isChange = false;
                }
              });
            }
            if (isChange) {
              this.ahpMult = this.jsmindData.ahp;
            }
            // 开始计算一下层次分析的数据
            this.ahpComp();
          } else {
            // 开始计算一下层次分析的数据
            this.ahpComp();
          }
        } else {
          this.$modal.msgError(response.msg);
        }
      });
    },
    // 递归获取表格
    getTable(data) {
      console.log(data);
      if (data.children) {
        this.ahpData.push(data.children);
        data.children.map((item) => {
          this.getTable(item);
        });
      }
    },
    // 给表格每个选项赋值
    getOneData() {
      let tableData = [];
      this.ahpData.map((item, index) => {
        // 普通列表加列
        tableData.push([]);
        this.ahpNumData.push({});
        item.map((itemChild, indexChild) => {
          // 最大特征值\cr\ci的循环列表值
          this.ahpNumData[index].max = "";
          this.ahpNumData[index].cr = "";
          this.ahpNumData[index].ci = "";
          tableData[index][indexChild] = [];
          tableData.map((e) => {
            item.map((item, i) => {
              // 普通列表加行
              tableData[index][indexChild][i] = 1;
            });
          });
        });
      });
      this.ahpMult = tableData;
    },
    // 计算层次分析
    ahpComp() {
      this.ahpData.map((item, index) => {
        let compList = [];
        item.map((itemChild) => {
          compList.push(itemChild.id);
        });
        let matrix = AHP.AHPMatrix.FromArray2d(compList, this.ahpMult[index]);
        this.$set(this.ahpNumData, index, {
          max: matrix.eigen().value,
          cr: matrix.CR(),
          ci: matrix.CI(),
        });
      });
    },
    // 上传层次分析
    ahpSubmit() {
      this.ahpData.map((item, index) => {
        let compList = [];
        item.map((itemChild) => {
          compList.push(itemChild.id);
        });
        let matrix = AHP.AHPMatrix.FromArray2d(compList, this.ahpMult[index]);
        matrix.render();
        // 赋予权重
        let weight = matrix.weight();
        if (matrix.consitstenceAvailable()) {
          this.ahpCompRecursion(this.jsmindData, weight);
        } else {
          // this.$modal.msgError(`表${index + 1}一致性不足，请重新计算`);
        }
        // 结构体加入ahp
        this.jsmindData.ahp = [];
        this.jsmindData.ahp = this.ahpMult;
        // 上传数据
      });
      this.updateZbtxData();
    },
    // 递归赋值
    ahpCompRecursion(data, weight) {
      let a = Object.keys(weight);
      if (Object.keys(weight).indexOf(data.id) !== -1) {
        data.weight = weight[data.id];
      }
      if (data.children) {
        data.children.map((item) => {
          this.ahpCompRecursion(item, weight);
        });
      }
    },
    // 上传数据
    updateZbtxData() {
      let updata = {
        id: this.mindId,
        indexId: this.$route.query.indexId,
        indexContent: JSON.stringify(this.jsmindData),
      };
      updateZbtxData(updata).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess("保存成功");
        } else {
          this.$modal.msgError(response.msg);
        }
      });
    },

    // 关闭弹窗的回调
    closeDialog() {
      this.ahpData = [];
      this.jsmindData = [];
      this.ahpData = [];
      this.ahpMult = [];
      this.ahpNumData = [];
    }
  },
};
</script>

<style lang="scss" scoped>
.ahp-detail-container {
  .ahp-detail-content {
    width: 100%;
    height: 650px;
    overflow: auto;

    .ahp-detail-content__item {
      margin-bottom: 20px;

      .ahp-detail-content__item_topline {
        color: var(--app-primary-base);
      }
    }
  }

  .ahp-detail-bottom {
    width: 100%;
    height: 100px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
